<?php

namespace App\Http\Controllers\Manager;

use App\Helper;
use App\Http\Controllers\Controller;
use App\Models\ApiLog;
use App\Models\ServerSpellMall;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ApiController extends Controller
{
    public $prefix='manager.server';

    public function index($id){
        return view($this->prefix.'.api',['id'=>$id]);
    }

    public function info($id){
        $server=$this->CheckServer($id);
        return JsonResponse::create(['err'=>0,'data'=>$server]);
    }
    public function getRandKey($id){
        $server=$this->CheckServer($id);
        $server->api_key=Helper::getRandStr();
        $server->save();
        return JsonResponse::create(['err'=>0,'data'=>$server]);
    }

    public function saveConfig($id,Request $request){
        $server=$this->CheckServer($id);
        $server->api_key=$request->get('api_key');
        $server->save();
        return JsonResponse::create(['err'=>0]);
    }

    public function getLogList($id,Request $request){
        $server=$this->CheckServer($id);

        $page=$request->get('page');
        $size=$request->get('size');

        $logs=ApiLog::where('server_id',$server->id)->orderBy('id','desc')->paginate($size);

        return [
            'err'=>0,
            'data'=>$logs
        ];
    }

}
